package com.jinke.api.modules.app.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinke.api.common.api.CommonPage;
import com.jinke.api.common.api.CommonResult;
import com.jinke.api.modules.app.controller.param.ContactUsReqParam;
import com.jinke.api.modules.app.request.MessageQueryRequest;
import com.jinke.api.modules.app.request.MessageReadRequest;
import com.jinke.api.modules.app.response.SysMessageVO;
import com.jinke.api.modules.app.service.SysMessageService;
import com.jinke.api.modules.ums.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/app/sysMessage")
@Api(value = "用户消息管理", tags = "用户消息管理")
public class SysMessageController {
    @Autowired
    private SysMessageService sysMessageService;

    @ApiOperation("消息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<SysMessageVO>> list(MessageQueryRequest request) {
        request.setUserId(UserUtil.getUserInfo().getId());
        Page<SysMessageVO> list = sysMessageService.list(request);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("是否有新消息")
    @RequestMapping(value = "/unread", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Long> unread() {
        return CommonResult.success(sysMessageService.unread());
    }

    @ApiOperation("标记已读")
    @RequestMapping(value = "/read", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult read(@RequestBody MessageReadRequest request) {
        return sysMessageService.read(request);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestBody MessageReadRequest request) {
        return sysMessageService.read(request);
    }


    /**
     * 用户反馈发送邮件到邮箱
     * @param contactUsReqParam
     * @return
     */
    @ApiOperation("用户反馈发送邮件到邮箱")
    @PostMapping("/contactUs")
    public CommonResult contactUs(@RequestBody @Validated ContactUsReqParam contactUsReqParam) {

        String htmlContent = "<body><p>来自<" + contactUsReqParam.getFirstName() + " "
                + contactUsReqParam.getLastName() + ">的消息</p><br/><p>公司名称: "
                + contactUsReqParam.getCompanyName() + "</p><br/><p>公司邮箱: "
                + contactUsReqParam.getMailAddress() + "</p><br/><p>电话号码: "
                + contactUsReqParam.getPhone() + "</p><br/><p>每月出货量: "
                + contactUsReqParam.getGoodsQuantity()
                + "</p><br/><p>我们能帮到您什么: "
                + contactUsReqParam.getHelpMessage() + "</p></body>";
        String subject="联系我们";
        return sysMessageService.sendMailToUs(htmlContent,null,subject,null);
    }
}

